var app = getApp()
const requireHttps = app.globalData.requireHttps //获取require域名
Page({
  data: {
    productInfo: {},
    imgs: [],
    file: [],
    describe: ''
  },
  onLoad: function() {
    let _this = this;
    // _this.uploadImg();
  },
  describe: function (e) {
    console.log(e.detail.value);
    this.data.describe = e.detail.value
  },
  //上传图片
  uploadImage: function(e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 12) {
      this.setData({
        lenMore: 1
      });
      setTimeout(function() {
        that.setData({
          lenMore: 0
        });
      }, 2500);
      return false;
    }
    wx.chooseImage({
      // count: 1, // 默认12
      sizeType: ['original', 'compressed'], // 指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var imgs = that.data.imgs;
        console.log(tempFilePaths + '----');
        let str = []
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (imgs.length >= 12) {
            // 显示模态窗口
            wx.showModal({
              title: '提示',
              content: '图片不能超过12个',
              success: function(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
            that.setData({
              imgs: imgs
            });
            return false;
          } else {
            imgs.push(tempFilePaths[i]);
            //上传图片
            wx.uploadFile({
              url: '' + requireHttps + '/api/publics/uploadImg', // 仅为示例，非真实的接口地址
              filePath: imgs[i],
              name: 'file',
              formData: {
                user: 'test'
              },
              success: res => {
                console.log(res)
                //转换成json
                const data = JSON.parse(res.data)
                //存储图片路径
                str.push(data.data.file)
                if (str.length == tempFilePaths.length){
                  that.setData({
                    file: str
                  })
                }
              }
            })
          }
        }
        console.log(imgs);
        that.setData({
          imgs: imgs
        });
      }
    });
  },
  // 删除图片
  deleteImg: function(e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function(e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
  goMyParking: function() {
    let _this = this;
    if (_this.data.describe == '' || _this.data.file == '') {
      wx.showModal({
        title: '提示',
        content: '请输入车位介绍或上传车位证件照',
        success: res => {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      // console.log(app.globalData.token);
      // console.log(app.globalData.parkingid);
      // console.log(_this.data.describe);
      // console.log(_this.data.file);
      let file = _this.data.file.join(',')
      console.log(file);
      wx.request({
        url: '' + requireHttps + '/api/member/userClaimParking',
        data: {
          token: app.globalData.token,
          gp_id: app.globalData.parkingid,
          claim_note: _this.data.describe,
          claim_img: file
        },
        method: 'post',
        success: res => {
          // console.log(res)
          // wx.showToast({
          //   title: '提交成功',
          // })
          wx.showModal({
            title: '提示',
            content: res.data.message,
            success: res => {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 3
                })
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 3
                })
              }
            }
          })
        }
      })
    }
  }
})